<?php
declare(strict_types=1);
namespace app\controller;

use think\facade\Db;
use app\model\Recruit as RecruitModel;
use app\model\User as UserModel;

class Recruit extends Base
{


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function recruitList()
    {
        $post = request()->param();
        $user_model = new RecruitModel();
        $res = $user_model->getAllByRow($post['list_rows'], false, ['query' => $post], $post);
        if ($res[0] !== true) {
            return $this->create($res[1] , '失败', 204);
        }
        return $this->create($res[1] , '成功');
    }

    public function submit()
    {
        $post = request()->param();
        $recruit_model = new RecruitModel();
        $user_model = new UserModel();
        // $res = [];
        // $get = $recruit_model->getAll()[1];
        // return $post[0];
        foreach($post as $keys=>$p){
            if($p['status']==="已通过"){
                $res[] = array(
                    'name'=>$p['name'],
                    'age'=>$p['age'],
                    'sex'=>$p['sex'],
                    'position'=>$p['position'],
                    'department'=>$p['department'],
                    'roles'=>'2',
                    'email'=>''
                );
            }
        }
        $test = $recruit_model->deleteAll();
        if ($test[0] !== true) {
            return $this->create($test[1] , '失败', 204);
        }
        // return $this->create($test , '成功');

        // unset($value);
        $res = $user_model->addAll($res);
        if ($res[0] !== true) {
            return $this->create($res[1] , '失败', 204);
        }
        return $this->create($res , '成功');
    }

    public function updateRoles()
    {
        $post = request()->param();
        $user_model = new RecruitModel();
        $user_model->updateRoles($post['work_num'], $post['roles']);
        return $this->create('', '成功');
    }

    public function updateStatus()
    {
        $post = request()->param();

        $user_model = new RecruitModel();
        $data = $user_model->updateStatus($post['id'], $post['status']);
        // return $data[1];
        if ($data[0] === 'false') {
            return $this->create($data[1], '失败', 20001);
        }
        return $this->create('', '成功');
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        // table方法必须指定完整的数据表名
        return 'hello';
    }
}